//
//  NewShopcartCell.m
//  FD
//
//  Created by xiaofang wang on 2018/11/20.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  受伤了不转模型了直接取

#import "NewShopcartCell.h"
#import "NewShopcartAddMinusView.h"
#import "NewShopcartModel.h" //模型

@interface NewShopcartCell()
/**背景*/
@property (nonatomic, strong) UIView *bgView;

/**标题和加减按钮的背景图片*/
@property (nonatomic, strong) UIView *nameAndAddMinuBgView;

/**1.标题*/
@property (nonatomic, strong)UILabel *nameLb;
/**2-3加减按钮*/
@property (nonatomic, strong) NewShopcartAddMinusView *addMinuView;



/**div2：€38.0/份数 €48.0    - 2 +*/
@property (nonatomic, strong) UIView *div2;
/**2-1现价*/
@property (nonatomic, strong) UILabel *currentPriceLb;
/**2-2原价*/
@property (nonatomic, strong) UILabel *originalPriceLb;




/**3.加料label 需要高度自适应 15号字体*/
@property (nonatomic, strong) UILabel *jiaLiaoLb;
/**4.备注*/
@property (nonatomic, strong) UILabel *remarkLb;
/**12-31灰色线*/
@property (nonatomic, strong) UIView *lineView;
@end

@implementation NewShopcartCell

#pragma mark - init

//cell的构造函数
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupView];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setupView  {
    [self.contentView addSubview:self.bgView];
    
    [self.bgView addSubview:self.nameAndAddMinuBgView];
    [self.nameAndAddMinuBgView addSubview:self.nameLb]; //标题
    [self.nameAndAddMinuBgView addSubview:self.addMinuView];//加减按钮
    

    [self.bgView addSubview:self.div2];
    [self.bgView addSubview:self.remarkLb]; //备注
    [self.bgView addSubview:self.jiaLiaoLb]; //加料
    
    
    //div2
    [self.div2 addSubview:self.currentPriceLb];
    [self.div2 addSubview:self.originalPriceLb];
//    [self.div2 addSubview:self.addMinuView];
    
    //12-31线
    [self.bgView addSubview:self.lineView];
}


#pragma mark - 二 layout
- (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat k15 = 15.0;
    //背景view
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    

    
    
    
    //1-19第一部分:标题 & 加减按钮
    [self.nameAndAddMinuBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(k15);
        make.right.equalTo(self.bgView.mas_right).offset(-k15);
        make.top.equalTo(self.bgView.mas_top).offset(10);
        make.height.mas_equalTo(20);
    }];
    //nameLb
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.nameAndAddMinuBgView);
        make.right.equalTo(self.addMinuView.mas_left);
    }];

    //12-31 加减按钮
    [self.addMinuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self.nameAndAddMinuBgView);
        make.width.mas_equalTo(65);
    }];
    


    
    [self.div2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameAndAddMinuBgView.mas_bottom);
        make.left.equalTo(self.bgView.mas_left).offset(k15);
        make.right.equalTo(self.bgView.mas_right).offset(-k15);
        make.height.equalTo(@30);
    }];

    //第三部分:配料 动态计算高度。换行
    [self.jiaLiaoLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(k15);
        make.right.equalTo(self.bgView.mas_right).offset(-k15);
        make.top.equalTo(self.div2.mas_bottom);
//        make.bottom.equalTo(self.remarkLb.mas_top);
//        make.height.mas_equalTo(30);
    }];
    
    //第四部分:备注
    [self.remarkLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(k15);
        make.right.equalTo(self.bgView.mas_right).offset(-k15);
        make.height.equalTo(@20);
        make.top.equalTo(self.jiaLiaoLb.mas_bottom);
//        make.bottom.equalTo(self.bgView.mas_bottom).offset(-10); //距离底部10px
    }];
    
    
    //d布局div2
    [self layoutDiv2];
    
    //12-31线
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.bgView);
        make.bottom.equalTo(self.bgView.mas_bottom).offset(-1);
        make.height.mas_equalTo(1);
    }];
}


- (void)layoutDiv2 {
    //w : 70
    [self.currentPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div2.mas_left);
        make.top.bottom.equalTo(self.div2);
        //masonry自适应宽度
        make.width.greaterThanOrEqualTo(@70); //大于等于70
    }];
    
    //60
    [self.originalPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.currentPriceLb.mas_right);
        make.top.bottom.equalTo(self.div2);
        make.width.equalTo(@60);
    }];
    
//    [self.addMinuView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.div2.mas_right);
//        make.top.bottom.equalTo(self.div2);
//        make.width.mas_equalTo(60);
//    }];
}

#pragma mark - 三 getter
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
    }
    return _bgView;
}


- (UIView *)nameAndAddMinuBgView {

    if (!_nameAndAddMinuBgView) {
        _nameAndAddMinuBgView = [UIView new];
//        _nameAndAddMinuBgView.backgroundColor = [UIColor lightGrayColor];
    }
    return _nameAndAddMinuBgView;
}


//标题
- (UILabel *)nameLb {
    if (!_nameLb) {
        _nameLb = [UILabel new];
        //1-2 打开用户交互,加减按钮是添加在nameLb上的.否则会导致点击事件无效
        _nameLb.userInteractionEnabled = YES;
        
//        _nameLb.backgroundColor = [UIColor yellowColor];
    }
    return _nameLb;
}

//!!!!:第二部分
- (UIView *)div2 {
    if (!_div2) {
        _div2 = [UIView new];
    }
    return _div2;
}


- (UILabel *)currentPriceLb {
    if (!_currentPriceLb) {
        _currentPriceLb = [UILabel new];
        _currentPriceLb.textColor = kMainRedColor;
    }
    return _currentPriceLb;
}

//中划线灰色文字
- (UILabel *)originalPriceLb {
    if (!_originalPriceLb) {
        _originalPriceLb = [UILabel new];
        _originalPriceLb.textColor = [UIColor lightGrayColor];
        _originalPriceLb.font = [UIFont systemFontOfSize:15.0];
    }
    return _originalPriceLb;
}
- (NewShopcartAddMinusView *)addMinuView {
    if (!_addMinuView) {
        _addMinuView = [[NewShopcartAddMinusView alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
        
        FDWeakSelf;
        //事件回调
        _addMinuView.minusBlock = ^{ //减➖
            if (weakSelf.newshopcartCellMinuBlock) {
                weakSelf.newshopcartCellMinuBlock();
            }
        };
        _addMinuView.addBlock = ^{ //加➕
            if (weakSelf.newshopcartCellAddBlock) {
                weakSelf.newshopcartCellAddBlock();
            };
        };
    }
    return _addMinuView;
}
//!!!!:第二部分





- (UILabel *)jiaLiaoLb {
    if (!_jiaLiaoLb) {
        _jiaLiaoLb = [UILabel new];
        _jiaLiaoLb.numberOfLines = 0; //自动换行
        _jiaLiaoLb.font = [UIFont systemFontOfSize:15.0];
    }
    return _jiaLiaoLb;
}

- (UILabel *)remarkLb {
    if (!_remarkLb) {
        _remarkLb = [UILabel new];
    }
    return _remarkLb;
}

//12-31灰色线
- (UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kMainLightGrayColor;
    }
    return _lineView;
}

#pragma mark - 四点击事件

#pragma mark - 五给cell赋值
- (void)setModel:(NewShopcartModel *)model {
    _model = model;
    
    NSDictionary *goodsDic = model.goods; //
    //1.标题

    self.nameLb.text = goodsDic[@"title"];
//    self.nameLb.text = @"大表哥大概发生的会计法里的水分开空间发达拉斯发";
    
    //2019-1-16数量
//    NSInteger quantity = [model.quantity integerValue];

    //2019-2-20 又不要了
    
    //2.就算原价和现价
    //2.div2 现价、原价、数量
    NSString *fenStr = NSLocalizedString(L_Order_Fen, @"份");

//    NSString *currentPriceString = [NSString stringWithFormat:@"€ %.1f/%@",model.totalcurrentPrice * quantity,fenStr];
    NSString *currentPriceString = [NSString stringWithFormat:@"€ %.1f/%@",model.totalcurrentPrice,fenStr];
    self.currentPriceLb.text = currentPriceString;
    
    
    //设置中划线
//    NSString *originPriceStr = [NSString stringWithFormat:@"€%.1f",model.totalOriginPrice * quantity];
    NSString *originPriceStr = [NSString stringWithFormat:@"€%.1f",model.totalOriginPrice];
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:originPriceStr attributes:attribtDic];
    self.originalPriceLb.attributedText = attribtStr;
    
    //2019-2-21 originalPriceLb显示逻辑
    // 原价>0 && 原价!=现价 才显示
    if (model.totalOriginPrice >0 &&
        (model.totalcurrentPrice != model.totalOriginPrice)) {
        self.originalPriceLb.hidden = NO;
    }else {
        self.originalPriceLb.hidden = YES;
    }
    

    //数量
    self.addMinuView.countLb.text = [NSString stringWithFormat:@"%@",model.quantity];
    
    //3. 加料
    NSArray *ingredientArr = model.goods[@"ingredientClassify"];
    NSString *jiaLiaoStr = [model jiaLiaoStrWithingredientClassifyArr:ingredientArr];
    self.jiaLiaoLb.text = jiaLiaoStr;
    
    //4.备注
    NSString *mark = model.remark ? : @"";
    
    NSString *bzStr = NSLocalizedString(Order_Remark, @"备注");
    self.remarkLb.text = [NSString stringWithFormat:@"%@: %@",bzStr,mark];
}
@end

